Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 534)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.94217 12.94290 12.94365 12.94440 12.94515 12.94589 12.94663 12.94734
## [9] 12.94805 12.94872 12.94937 12.94999 12.95056 12.95110 12.95159 12.95202
## [17] 12.95240 12.95272 12.95298 12.95316 12.95327 12.95330 12.95325 12.95311
## [25] 12.95287 12.95254 12.95210 12.95156 12.95091 12.95014 12.94925 12.94827
## [33] 12.94718 12.94601 12.94475 12.94341 12.94200 12.94054 12.93901 12.93744
## [41] 12.93583 12.93418 12.93251 12.93081 12.92910 12.92739 12.92568 12.92397
## [49] 12.92229 12.92062 12.91898 12.91738 12.91583 12.91432 12.91287 12.91149
## [57] 12.91018 12.90883 12.90733 12.90570 12.90394 12.90207 12.90010 12.89804
## [65] 12.89590 12.89370 12.89143 12.88913 12.88679 12.88444 12.88207 12.87970
## [73] 12.87735 12.87503 12.87274 12.87050 12.86831 12.86620 12.86417 12.86224
## [81] 12.86041 12.85870 12.85712 12.85567 12.85438 12.85265 12.84995 12.84634
## [89] 12.84191 12.83673 12.83089 12.82446 12.81751 12.81013 12.80239 12.79438
## [97] 12.78616 12.77781 12.76942 12.76106 12.75280 12.74474 12.73693 12.72947
## [105] 12.72242 12.71587 12.70989 12.70457 12.69997 12.69618 12.69327 12.69132
## [113] 12.69042 12.69000 12.68950 12.68895 12.68838 12.68783 12.68734 12.68692
## [121] 12.68663 12.68649 12.68654 12.68682 12.68735 12.68817 12.68931 12.69081
## [129] 12.69271 12.69504 12.69782 12.70110 12.70491 12.70928 12.71426 12.72103
## [137] 12.73051 12.74231 12.75600 12.77119 12.78748 12.80445 12.82170 12.83882
## [145] 12.85542 12.87109 12.88542 12.89800 12.90843 12.91886 12.93146 12.94586
## [153] 12.96170 12.97862 12.99625 13.01424 13.03222 13.04982 13.06669 13.08245
## [161] 13.09675 13.10923 13.11951 13.13024 13.14400 13.16043 13.17912 13.19970
## [169] 13.22176 13.24494 13.26883 13.29305 13.31721 13.34092 13.36380 13.38546
## [177] 13.40551 13.42356 13.43923 13.45212 13.46185 13.46803 13.47229 13.47645
## [185] 13.48047 13.48427 13.48779 13.49098 13.49376 13.49607 13.49786 13.49905
## [193] 13.49959 13.49941 13.49845 13.49665 13.49393 13.49025 13.48553 13.47972
## [201] 13.47275 13.46455 13.45507 13.44424 13.43116 13.41523 13.39684 13.37639
## [209] 13.35428 13.33088 13.30661 13.28185 13.25701 13.23246 13.20861 13.18585
## [217] 13.16458 13.14148 13.11349 13.08134 13.04581 13.00763 12.96756 12.92635
## [225] 12.88475 12.84352 12.80341 12.76516 12.72953 12.69727 12.66913 12.64080
## [233] 12.60801 12.57154 12.53220 12.49077 12.44805 12.40484 12.36193 12.32012
## [241] 12.28020 12.24297 12.20922 12.17975 12.15535 12.13403 12.11327 12.09310
## [249] 12.07353 12.05459 12.03630 12.01868 12.00175 11.98554 11.97006 11.95534
## [257] 11.94139 11.92824 11.91592 11.90602 11.89977 11.89665 11.89611 11.89762
## [265] 11.90066 11.90467 11.90913 11.91350 11.91725 11.91984 11.92074 11.91942
## [273] 11.91533 11.91136 11.91041 11.91199 11.91557 11.92066 11.92674 11.93331
## [281] 11.93987 11.94590 11.95090 11.95436 11.95578 11.95464 11.95045 11.94542
## [289] 11.94196 11.93977 11.93859 11.93813 11.93811 11.93827 11.93831 11.93797
## [297] 11.93696 11.93500 11.93183 11.92716 11.92071 11.91257 11.90312 11.89251
## [305] 11.88089 11.86842 11.85525 11.84154 11.82743 11.81308 11.79865 11.78429
## [313] 11.77015 11.75638 11.74314 11.73058 11.71887 11.70474 11.68544 11.66186
## [321] 11.63490 11.60543 11.57434 11.54254 11.51089 11.48029 11.45163 11.42579
## [329] 11.40367 11.38616 11.37413 11.36362 11.35044 11.33517 11.31835 11.30057
## [337] 11.28238 11.26435 11.24704 11.23102 11.21686 11.20512 11.19637 11.19117
## [345] 11.19008 11.19213 11.19588 11.20119 11.20792 11.21596 11.22517 11.23541
## [353] 11.24656 11.25848 11.27104 11.28412 11.29758 11.31129 11.32511 11.34063
## [361] 11.35929 11.38080 11.40486 11.43116 11.45940 11.48928 11.52051 11.55278
## [369] 11.58578 11.61923 11.65281 11.68623 11.71919 11.75138 11.78251 11.81226
## [377] 11.84035 11.86647 11.89403 11.92611 11.96197 12.00085 12.04199 12.08464
## [385] 12.12805 12.17145 12.21409 12.25522 12.29409 12.32992 12.36198 12.38950
## [393] 12.41678 12.44811 12.48272 12.51985 12.55875 12.59865 12.63879 12.67841
## [401] 12.71676 12.75306 12.78657 12.81651 12.84214 12.86268 12.88029 12.89756
## [409] 12.91438 12.93066 12.94630 12.96120 12.97526 12.98838 13.00047 13.01142
## [417] 13.02113 13.02951 13.03646 13.04187 13.04485 13.04479 13.04205 13.03695
## [425] 13.02987 13.02113 13.01108 13.00008 12.98847 12.97659 12.96479 12.95342
## [433] 12.94283 12.93335 12.92534 12.91578 12.90188 12.88436 12.86396 12.84137
## [441] 12.81734 12.79256 12.76777 12.74368 12.72101 12.70048 12.68282 12.66873
## [449] 12.65461 12.63678 12.61587 12.59250 12.56728 12.54083 12.51379 12.48676
## [457] 12.46038 12.43526 12.41203 12.39130 12.37370 12.35985 12.34800 12.33605
## [465] 12.32408 12.31214 12.30031 12.28867 12.27728 12.26621 12.25554 12.24533
## [473] 12.23566 12.22660 12.21822 12.21058 12.20382 12.19794 12.19285 12.18844
## [481] 12.18463 12.18131 12.17838 12.17576 12.17334 12.17103 12.16873 12.16634
## [489] 12.16376 12.16090 12.15810 12.15573 12.15378 12.15221 12.15101 12.15013
## [497] 12.14956 12.14927 12.14923 12.14942 12.14981 12.15037 12.15108 12.15191
## [505] 12.15294 12.15429 12.15593 12.15787 12.16009 12.16259 12.16537 12.16841
## [513] 12.17170 12.17526 12.17905 12.18308 12.18734 12.19183 12.19653 12.20144
## [521] 12.20651 12.21169 12.21700 12.22245 12.22806 12.23385 12.23983 12.24601
## [529] 12.25241 12.25904 12.26593 12.27308 12.28051 12.28823
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 534)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.57826 12.57605 12.57391 12.57185 12.56986 12.56794 12.56608 12.56428
## [9] 12.56254 12.56086 12.55922 12.55764 12.55611 12.55462 12.55317 12.55176
## [17] 12.55038 12.54903 12.54772 12.54643 12.54516 12.54392 12.54269 12.54147
## [25] 12.54027 12.53907 12.53788 12.53669 12.53550 12.53430 12.53305 12.53179
## [33] 12.53050 12.52920 12.52789 12.52658 12.52527 12.52397 12.52269 12.52143
## [41] 12.52020 12.51901 12.51786 12.51676 12.51571 12.51472 12.51379 12.51294
## [49] 12.51217 12.51148 12.51088 12.51038 12.50998 12.50969 12.50952 12.50947
## [57] 12.50955 12.50966 12.50973 12.50976 12.50977 12.50975 12.50973 12.50970
## [65] 12.50969 12.50970 12.50974 12.50982 12.50994 12.51013 12.51038 12.51072
## [73] 12.51114 12.51166 12.51228 12.51303 12.51390 12.51490 12.51605 12.51736
## [81] 12.51884 12.52048 12.52232 12.52434 12.52657 12.52874 12.53058 12.53212
## [89] 12.53340 12.53444 12.53528 12.53593 12.53643 12.53682 12.53711 12.53733
## [97] 12.53752 12.53771 12.53792 12.53819 12.53853 12.53899 12.53959 12.54035
## [105] 12.54131 12.54250 12.54395 12.54568 12.54772 12.55011 12.55287 12.55603
## [113] 12.55962 12.56311 12.56601 12.56840 12.57037 12.57199 12.57335 12.57454
## [121] 12.57563 12.57671 12.57786 12.57916 12.58070 12.58256 12.58482 12.58757
## [129] 12.59088 12.59484 12.59953 12.60504 12.61144 12.61998 12.63154 12.64566
## [137] 12.66188 12.67974 12.69879 12.71857 12.73862 12.75848 12.77769 12.79581
## [145] 12.81236 12.82689 12.83894 12.85188 12.86891 12.88938 12.91262 12.93798
## [153] 12.96477 12.99235 13.02004 13.04719 13.07313 13.09720 13.11873 13.13707
## [161] 13.15154 13.16557 13.18266 13.20224 13.22371 13.24653 13.27009 13.29385
## [169] 13.31721 13.33961 13.36047 13.37922 13.39528 13.40808 13.41705 13.42367
## [177] 13.42982 13.43548 13.44061 13.44519 13.44921 13.45264 13.45545 13.45763
## [185] 13.45915 13.45999 13.46013 13.45953 13.45819 13.45607 13.45316 13.44943
## [193] 13.44485 13.43941 13.43308 13.42585 13.41767 13.40855 13.39844 13.38733
## [201] 13.37519 13.36201 13.34776 13.33241 13.31435 13.29239 13.26716 13.23926
## [209] 13.20933 13.17797 13.14581 13.11347 13.08157 13.05072 13.02155 12.99468
## [217] 12.97072 12.94544 12.91480 12.87966 12.84092 12.79944 12.75612 12.71183
## [225] 12.66746 12.62388 12.58197 12.54262 12.50671 12.47512 12.44873 12.42358
## [233] 12.39553 12.36514 12.33300 12.29968 12.26576 12.23180 12.19839 12.16610
## [241] 12.13551 12.10719 12.08171 12.05966 12.04161 12.02671 12.01361 12.00213
## [249] 11.99207 11.98327 11.97553 11.96868 11.96253 11.95689 11.95159 11.94643
## [257] 11.94125 11.93585 11.93005 11.92628 11.92670 11.93068 11.93760 11.94682
## [265] 11.95773 11.96971 11.98212 11.99434 12.00576 12.01575 12.02367 12.02892
## [273] 12.03086 12.03347 12.04061 12.05153 12.06544 12.08158 12.09916 12.11743
## [281] 12.13561 12.15292 12.16859 12.18186 12.19195 12.19808 12.19949 12.19890
## [289] 12.19937 12.20063 12.20245 12.20454 12.20667 12.20856 12.20997 12.21063
## [297] 12.21028 12.20867 12.20554 12.20063 12.19369 12.18377 12.17041 12.15405
## [305] 12.13514 12.11412 12.09145 12.06756 12.04291 12.01793 11.99308 11.96881
## [313] 11.94555 11.92375 11.90387 11.88634 11.87162 11.85593 11.83574 11.81181
## [321] 11.78493 11.75586 11.72538 11.69427 11.66330 11.63324 11.60486 11.57895
## [329] 11.55627 11.53761 11.52372 11.51112 11.49612 11.47924 11.46100 11.44191
## [337] 11.42250 11.40328 11.38477 11.36747 11.35192 11.33862 11.32810 11.32086
## [345] 11.31744 11.31644 11.31615 11.31661 11.31785 11.31990 11.32279 11.32656
## [353] 11.33123 11.33684 11.34342 11.35100 11.35961 11.36928 11.38005 11.39330
## [361] 11.41015 11.43025 11.45324 11.47878 11.50652 11.53609 11.56715 11.59935
## [369] 11.63233 11.66575 11.69924 11.73246 11.76506 11.79668 11.82697 11.85558
## [377] 11.88215 11.90633 11.93160 11.96117 11.99433 12.03035 12.06851 12.10809
## [385] 12.14838 12.18865 12.22818 12.26626 12.30216 12.33517 12.36456 12.38961
## [393] 12.41346 12.43945 12.46716 12.49616 12.52605 12.55638 12.58675 12.61673
## [401] 12.64591 12.67385 12.70015 12.72438 12.74612 12.76494 12.78273 12.80146
## [409] 12.82086 12.84066 12.86060 12.88039 12.89977 12.91847 12.93622 12.95275
## [417] 12.96779 12.98106 12.99230 13.00125 13.00788 13.01252 13.01533 13.01649
## [425] 13.01619 13.01461 13.01192 13.00830 13.00394 12.99902 12.99371 12.98820
## [433] 12.98266 12.97727 12.97222 12.96615 12.95776 12.94738 12.93531 12.92186
## [441] 12.90732 12.89202 12.87625 12.86033 12.84455 12.82924 12.81468 12.80120
## [449] 12.78626 12.76751 12.74558 12.72106 12.69456 12.66669 12.63805 12.60926
## [457] 12.58092 12.55364 12.52802 12.50468 12.48421 12.46722 12.45043 12.43051
## [465] 12.40807 12.38370 12.35799 12.33155 12.30497 12.27885 12.25378 12.23036
## [473] 12.20918 12.19085 12.17595 12.16508 12.15632 12.14742 12.13851 12.12973
## [481] 12.12120 12.11306 12.10544 12.09846 12.09227 12.08698 12.08274 12.07967
## [489] 12.07791 12.07758 12.07809 12.07880 12.07981 12.08118 12.08300 12.08537
## [497] 12.08835 12.09203 12.09650 12.10183 12.10812 12.11544 12.12387 12.13350
## [505] 12.14407 12.15525 12.16707 12.17956 12.19273 12.20661 12.22121 12.23656
## [513] 12.25268 12.26959 12.28732 12.30588 12.32530 12.34559 12.36678 12.38889
## [521] 12.41202 12.43621 12.46143 12.48765 12.51484 12.54297 12.57199 12.60188
## [529] 12.63260 12.66412 12.69641 12.72942 12.76314 12.79752
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 534)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.88647 11.88819 11.88994 11.89171 11.89349 11.89527 11.89705 11.89882
## [9] 11.90057 11.90230 11.90399 11.90564 11.90725 11.90881 11.91030 11.91172
## [17] 11.91307 11.91434 11.91551 11.91659 11.91756 11.91843 11.91917 11.91979
## [25] 11.92028 11.92062 11.92082 11.92086 11.92074 11.92046 11.91999 11.91935
## [33] 11.91851 11.91747 11.91623 11.91478 11.91309 11.91115 11.90897 11.90658
## [41] 11.90399 11.90122 11.89829 11.89522 11.89203 11.88873 11.88535 11.88189
## [49] 11.87839 11.87485 11.87129 11.86775 11.86422 11.86074 11.85731 11.85396
## [57] 11.85070 11.84756 11.84455 11.84169 11.83900 11.83650 11.83420 11.83212
## [65] 11.82975 11.82659 11.82270 11.81816 11.81301 11.80734 11.80120 11.79465
## [73] 11.78777 11.78060 11.77323 11.76570 11.75809 11.75045 11.74286 11.73538
## [81] 11.72806 11.72098 11.71419 11.70777 11.70177 11.69625 11.69129 11.68695
## [89] 11.68329 11.68037 11.67825 11.67701 11.67569 11.67336 11.67012 11.66607
## [97] 11.66130 11.65592 11.65003 11.64372 11.63710 11.63025 11.62329 11.61631
## [105] 11.60941 11.60269 11.59624 11.59017 11.58457 11.57955 11.57520 11.57163
## [113] 11.56892 11.56718 11.56651 11.56701 11.56877 11.57190 11.57650 11.58265
## [121] 11.59114 11.60243 11.61617 11.63199 11.64956 11.66852 11.68851 11.70918
## [129] 11.73018 11.75116 11.77176 11.79164 11.81043 11.82778 11.84335 11.86035
## [137] 11.88179 11.90698 11.93523 11.96586 11.99818 12.03151 12.06516 12.09845
## [145] 12.13069 12.16119 12.18927 12.21425 12.23543 12.25664 12.28171 12.30998
## [153] 12.34082 12.37355 12.40753 12.44212 12.47665 12.51047 12.54293 12.57338
## [161] 12.60117 12.62564 12.64615 12.66511 12.68524 12.70635 12.72822 12.75065
## [169] 12.77344 12.79638 12.81926 12.84188 12.86404 12.88552 12.90613 12.92565
## [177] 12.94389 12.96063 12.97567 12.98881 12.99984 13.00856 13.01612 13.02377
## [185] 13.03139 13.03890 13.04619 13.05316 13.05972 13.06575 13.07116 13.07586
## [193] 13.07973 13.08268 13.08462 13.08543 13.08502 13.08329 13.08014 13.07547
## [201] 13.06917 13.06116 13.05132 13.03956 13.02460 13.00561 12.98320 12.95793
## [209] 12.93040 12.90120 12.87091 12.84011 12.80939 12.77934 12.75054 12.72358
## [217] 12.69905 12.67217 12.63854 12.59925 12.55539 12.50808 12.45840 12.40747
## [225] 12.35638 12.30623 12.25812 12.21315 12.17242 12.13704 12.10810 12.08079
## [233] 12.05001 12.01642 11.98067 11.94344 11.90536 11.86711 11.82935 11.79272
## [241] 11.75789 11.72552 11.69627 11.67079 11.64974 11.63166 11.61459 11.59845
## [249] 11.58317 11.56869 11.55493 11.54182 11.52929 11.51727 11.50568 11.49447
## [257] 11.48354 11.47284 11.46230 11.45403 11.44982 11.44906 11.45116 11.45551
## [265] 11.46151 11.46856 11.47607 11.48343 11.49003 11.49529 11.49859 11.49934
## [273] 11.49694 11.49589 11.50046 11.50971 11.52270 11.53849 11.55615 11.57473
## [281] 11.59330 11.61092 11.62664 11.63954 11.64866 11.65307 11.65183 11.64741
## [289] 11.64288 11.63821 11.63335 11.62826 11.62291 11.61727 11.61128 11.60491
## [297] 11.59813 11.59090 11.58317 11.57491 11.56609 11.55421 11.53728 11.51602
## [305] 11.49111 11.46327 11.43320 11.40160 11.36917 11.33663 11.30467 11.27399
## [313] 11.24530 11.21931 11.19671 11.17822 11.16452 11.15146 11.13484 11.11532
## [321] 11.09357 11.07026 11.04605 11.02162 10.99762 10.97474 10.95362 10.93494
## [329] 10.91937 10.90758 10.90022 10.89586 10.89257 10.89031 10.88903 10.88868
## [337] 10.88922 10.89060 10.89276 10.89567 10.89928 10.90353 10.90839 10.91380
## [345] 10.91972 10.92724 10.93725 10.94948 10.96360 10.97932 10.99635 11.01437
## [353] 11.03310 11.05222 11.07143 11.09044 11.10895 11.12664 11.14323 11.16049
## [361] 11.18022 11.20217 11.22608 11.25172 11.27881 11.30712 11.33638 11.36635
## [369] 11.39677 11.42739 11.45796 11.48822 11.51793 11.54682 11.57466 11.60118
## [377] 11.62613 11.64926 11.67257 11.69798 11.72513 11.75363 11.78313 11.81324
## [385] 11.84361 11.87385 11.90361 11.93250 11.96016 11.98622 12.01030 12.03204
## [393] 12.05357 12.07703 12.10204 12.12821 12.15517 12.18253 12.20992 12.23695
## [401] 12.26325 12.28843 12.31212 12.33392 12.35348 12.37039 12.38661 12.40409
## [409] 12.42254 12.44163 12.46105 12.48047 12.49958 12.51808 12.53563 12.55192
## [417] 12.56664 12.57946 12.59009 12.59818 12.60370 12.60695 12.60819 12.60765
## [425] 12.60559 12.60225 12.59788 12.59271 12.58701 12.58100 12.57494 12.56908
## [433] 12.56365 12.55891 12.55509 12.55021 12.54239 12.53211 12.51984 12.50602
## [441] 12.49114 12.47565 12.46002 12.44471 12.43019 12.41692 12.40537 12.39600
## [449] 12.38649 12.37447 12.36035 12.34451 12.32735 12.30927 12.29065 12.27190
## [457] 12.25341 12.23558 12.21879 12.20345 12.18995 12.17868 12.16832 12.15735
## [465] 12.14589 12.13407 12.12199 12.10979 12.09758 12.08547 12.07360 12.06207
## [473] 12.05100 12.04052 12.03074 12.02178 12.01331 12.00492 11.99661 11.98842
## [481] 11.98035 11.97243 11.96466 11.95708 11.94969 11.94251 11.93556 11.92886
## [489] 11.92242 11.91625 11.91031 11.90452 11.89889 11.89343 11.88813 11.88302
## [497] 11.87808 11.87334 11.86879 11.86444 11.86030 11.85637 11.85266 11.84917
## [505] 11.84591 11.84284 11.83997 11.83729 11.83482 11.83253 11.83044 11.82854
## [513] 11.82682 11.82530 11.82396 11.82280 11.82183 11.82104 11.82043 11.81999
## [521] 11.81973 11.81965 11.81975 11.82003 11.82049 11.82113 11.82195 11.82295
## [529] 11.82413 11.82549 11.82704 11.82877 11.83069 11.83279
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")